﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNADash.BoardBlocks
{
    public class EnemyBlock : BaseBlock
    {
        protected override Textures.GameTexture BlockTexture
        {
            get
            {
                return Textures.GameTexture.Enemy;
            }
        }

        private Directions Direction = Directions.N;

        public override void ApplyPhysics()
        {
            this.ExplodeIfMust();

            // go!
            Directions possibleDirection = PickPrevMoveDirection( this.Direction );            
            for ( int i=0; i<4; i++ )
            {
                if ( !this.TryToMove( possibleDirection ) )
                    possibleDirection = PickNextMoveDirection( possibleDirection );
                else
                {
                    this.Direction = possibleDirection;
                    break;
                }
            }
        }

        private bool TryToMove( Directions MoveDirection )
        {
            BaseBlock block = this.GetNeighbour( MoveDirection );
            if ( block == null )
            {
                this.MoveTo( MoveDirection );
                return true;
            }
            if ( block is PlayerBlock )
            {
                block.ExplodeNeighbour( Directions.None );
                return true;
            }

            return false;
        }

        public static Directions PickPrevMoveDirection( Directions CurrentDirection )
        {
            switch ( CurrentDirection )
            {
                case Directions.N:
                    return Directions.W;
                case Directions.W:
                    return Directions.S;
                case Directions.S:
                    return Directions.E;
                case Directions.E:
                    return Directions.N;

                default:
                    return Directions.None;
            }
        }

        public static Directions PickNextMoveDirection( Directions CurrentDirection )
        {
            switch ( CurrentDirection )
            {
                case Directions.N :
                    return Directions.E;
                case Directions.E :
                    return Directions.S;
                case Directions.S :
                    return Directions.W;
                case Directions.W :
                    return Directions.N;

                default:
                    return Directions.None;
            }
        }
    }
}
